# Maintainer: Martell Malone <martellmalone@gmail.com>

MINGW_PACKAGE_PREFIX=mingw-w64-cross

_realname=binutils
_base_ver=2.24.51
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.24.51.81480.2ddf430
pkgrel=1
pkgdesc="A set of programs to assemble and manipulate binary and object files"
arch=('i686' 'x86_64')
url="https://www.gnu.org/software/binutils/"
license=('GPL')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain" "${MINGW_PACKAGE_PREFIX}")
depends=("libiconv" "zlib")
makedepends=("libiconv" "zlib" "git")
options=('staticlibs' '!distcc' '!ccache' '!buildflags')


source=("${_realname}"::"git://sourceware.org/git/binutils-gdb.git#branch=master"
        '0001-Patch-early-arm-pe.patch'
        '0002-Continue-on-it.patch'
        '0003-msys2-support-for-binutils.patch'
        )

sha256sums=('SKIP'
            '64007e386cf1243e53ecbd9e929b79ee170cda005489121badd6e6c6efc85d65'
            'd1dd197bf91b2d3c35c3fb6c93e2a7ca0074a75f48c5adb03ec75fd554228967'
            '100be247c25eb80bd5dfcdfd4d29c1f6b47db09f8418b72f1278f939392d81b2')

#_targets="i686-w64-mingw32 x86_64-w64-mingw32 armv7-w64-mingw32ce"
_targets="armv7-w64-mingw32ce"

pkgver() {
  cd "$srcdir/$_realname"
  printf "%s.%s.%s" "${_base_ver}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/${_realname}

  git am ${srcdir}/0001-Patch-early-arm-pe.patch
  git am ${srcdir}/0002-Continue-on-it.patch
  git am ${srcdir}/0003-msys2-support-for-binutils.patch


  #do not install libiberty
  #sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
  # hack! - libiberty configure tests for header files using "$CPP $CPPFLAGS"
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" libiberty/configure

  mkdir ${srcdir}/binutils-build
}

build() {
  for _target in ${_targets}; do
  mkdir -p ${srcdir}/binutils-build-${_target} && cd ${srcdir}/binutils-build-${_target}

  if [ "${_target}" = "x86_64-w64-mingw32" ]; then
    local _conf='--enable-64-bit-bfd'
  else
    local _conf=''
  fi

  ${srcdir}/${_realname}/configure \
  --build=${CHOST} \
  --host=${CHOST} \
  --target=${_target} \
  --prefix=/opt \
  --disable-werror \
  --enable-lto \
  --with-libiconv-prefix=/usr \
  --disable-gdb \
  --disable-sim \
  --disable-readline \
  --disable-libdecimal \
  $_conf

  make
  done
}

check() {
  for _target in ${_targets}; do
  cd ${srcdir}/binutils-build-${_target}

  # unset LDFLAGS as testsuite makes assumptions about which ones are active
  # do not abort on errors - manually check log files
  make LDFLAGS="" -k check || true
  done
}

package() {
  for _target in ${_targets}; do
  cd ${srcdir}/binutils-build-${_target}
  make DESTDIR=${pkgdir} install

  done
}
